package com.ctv.my.customview.MaiCustomView.CitySelector.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.ctv.my.customview.MaiCustomView.CitySelector.bean.City;
import com.ctv.my.customview.MaiCustomView.CitySelector.bean.Province;
import com.ctv.my.customview.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ms on 2017/7/5.
 */

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private final static int LAYOUT = R.layout.city_select_province_view;//父级布局，省
    private final static int LAYOUT_CHILD = R.layout.city_selecter_city_view;//子布局，城市
    private LayoutInflater inflater;
    private List<Province> mList = new ArrayList<>();

    public MyExpandableListViewAdapter(Context context, List<Province> list) {
        this.mContext = context;
        this.mList = list;
        inflater = LayoutInflater.from(context);
    }

    //  获得父项的数量
    @Override
    public int getGroupCount() {
        return mList.size();
    }

    //  获得某个父项的子项数目
    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).getCity().size();
    }

    //  获得某个父项
    @Override
    public Object getGroup(int groupPosition) {
        return mList.get(groupPosition);
    }

    //  获得某个父项的某个子项
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).getCity().get(childPosition);
    }

    //  获得某个父项的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //  获得某个父项的某个子项的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolderGroup();
            convertView = inflater.inflate(LAYOUT, null);
            viewHolder.tevProvince = (TextView) convertView.findViewById(R.id.province);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolderGroup) convertView.getTag();
        }
        Province province = mList.get(groupPosition);
        viewHolder.tevProvince.setText(province.getSortLetters() + " - " + province.getName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderChild viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(LAYOUT_CHILD, null);
            viewHolder = new ViewHolderChild();
            viewHolder.tevCity = (TextView) convertView.findViewById(R.id.city);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolderChild) convertView.getTag();
        }
        City city = mList.get(groupPosition).getCity().get(childPosition);
        viewHolder.tevCity.setText(city.getName());
        return convertView;
    }

    //子项是否可选中，如果需要设置子项的点击事件，需要返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ViewHolderGroup {
        TextView tevProvince;
    }

    class ViewHolderChild {
        TextView tevCity;
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getGroupCount(); i++) {
            String sortStr = mList.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }
}
